import json
import os

from data.dataType.ItemType import ItemType


class Config:
    def __init__(self):
        self.rootPath = os.path.abspath('..')  # 程序运行路径
        self.configPath = self.rootPath + "\\config\\Config.json"  # Config 文件路径
        self.readerConfigPath = self.rootPath + "\\config\\ReaderConfig.json"  # ReaderConfig 文件路径
        self.parametersConfigPath = self.rootPath + "\\config\\ParametersConfig.json"  # ParametersConfig 文件路径
        self.itemType = None
        self.configJson = None  # configJson 對象
        self.readConfigJson = None  # readConfigJson 對象
        self.parametersConfigJson = None  # parametersConfigJson 對象
        self.exclList = None  # 可选EXCEL列表
        self.panelList = None  # 主题列表
        self.blackPanel = None  # 默认主题
        self.highColor = None  # 高亮字体颜色
        self.highSize = None  # 高亮字体大小
        self.searchSelect = None  # 默认检索EXCEL
        self.searchExclPath = None  # 默认EXCEL路径
        self.tacticsExclList = None  # 策略EXCEL列表
        self.tacticsSelect = None  # 默认选择策略
        self.tacticsList = None  # 策略列表
        self.histryPath = None  # 所有历史路径
        self.eInfo = []  # 错误信息

    def SetDefault(self):
        """
        读取所有配置文件，设置配置的默认值\n
        :return:成功返回True
        """
        isOk1 = self.SetConfig()
        isOk2 = self.SetReaderConfig()
        isOk3 = self.SetItemType()
        isOK4 = self.SetParameters()
        isOk = all([isOk1, isOk2, isOk3, isOK4])
        return isOk

    def SetConfig(self):
        """
        读取默认设置文件，设置默认配置值\n
        :return: 成功返回True
        """
        try:
            with open(self.configPath, "r", encoding="utf-8") as f:
                self.configJson = configJson = json.load(f)
                self.blackPanel = configJson["主题模式"]
                self.searchSelect = configJson["查询默认Excel"]
                self.searchExclPath = configJson["默认路径"]
                self.highColor = configJson["高亮颜色"]
                self.highSize = configJson['字体大小']
                self.tacticsExclList = list(configJson["策略选择"].values())[0]
                self.tacticsSelect = list(configJson["策略选择"].keys())[0]
                self.tacticsList = list(configJson["策略选择"].keys())
                self.histryPath = configJson["历史路径"]
            return True
        except FileNotFoundError:
            self.eInfo.append("Config.py: SetConfig => config 读取失败！")
            return False

    def SetReaderConfig(self):
        """
        读取EXCEL缓存格式，设置默认配置值\n
        :return: 成功返回True
        """
        try:
            with open(self.readerConfigPath, "r", encoding="utf-8") as f:
                self.readConfigJson = json.load(f)
                self.exclList = list(self.readConfigJson.keys())
                self.panelList = ["dark_teal", "light_blue", "light_blue_white", "default"]
                configJson = self.configJson
                # self.exclList.pop(0)
                # self.exclList.remove(configJson["查询默认Excel"])
                # self.exclList.insert(0, configJson["查询默认Excel"])
                self.panelList.remove(configJson["主题模式"])
                self.panelList.insert(0, configJson["主题模式"])
            return True
        except FileNotFoundError:
            self.eInfo.append("Config.py: SetReaderConfig => readerConfig 读取失败！")
            return False

    def SetItemType(self):
        self.itemType = ItemType()
        isOk = self.itemType.SetExcelReader(self.readConfigJson)
        if not isOk:
            self.eInfo.append(''.join(self.itemType.erro))
        return isOk

    def SetParameters(self):
        try:
            with open(self.parametersConfigPath, 'r',encoding="utf-8") as f:
                self.parametersConfigJson = json.load(f)
            return True
        except FileNotFoundError:
            self.eInfo.append("Config.py: SetParameters => parametersConfig 读取失败！")
            return False

    def SaveDefualtConfig(self, dataList):
        """
        将列表中的值写入默认设置中\n
        :param dataList:需要修改的默认设置列表
        :return:成功返回True
        """
        try:
            wPath = self.configPath
            configJson = self.configJson
            configJson["主题模式"] = dataList[0]
            configJson["查询默认Excel"] = dataList[1]
            configJson["默认路径"] = dataList[2]
            configJson["高亮颜色"] = dataList[3]
            configJson["字体大小"] = dataList[4]
            configJson["历史路径"] = dataList[5]
            with open(wPath, "w", encoding="utf-8") as f:
                f.write(json.dumps(configJson, ensure_ascii=False))
            return True
        except FileNotFoundError:
            self.eInfo.append("Config.py: SaveExcelConfig => 默认配置文件保存失败！")
            return False

    def SaveHistoryPath(self, allPathList):
        """
        将列表中的历史路径写入默认设置文件中\n
        :param allPathList:历史路径列表
        :return:成功返回True
        """
        try:
            wPath = self.configPath
            configJson = self.configJson
            configJson["历史路径"] = allPathList
            with open(wPath, "w", encoding="utf-8") as f:
                f.write(json.dumps(configJson, ensure_ascii=False))
            return True
        except FileNotFoundError:
            self.eInfo.append("Config.py: SaveExcelConfig => 默认配置文件保存失败！")
            return False
